﻿package ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.Enumeration;

import javax.swing.AbstractButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import components.MyButton;
import components.MyFrame;
import components.MyPanel;



import salesManagerLogic.MockSalesManagerLogic;
import vo.salesManager.BookQueryPanelVO;
import vo.salesManager.CustomerQueryPanelVO;

import logicInterface.SalesManagerLogicInterface;
import main.Main;


public class BookQueryPanel extends MyPanel{
	
	private MyButton logoutButton;
	private MyButton returnbackButton;
	
	private JLabel booklistLabel;
	private JLabel booksearchLabel;
	private DefaultTableModel bookdtm;
	private JTable booklist;
	private JScrollPane bookjsp;
	private JTextField booksearchField;
	private JLabel classLabel;
	private JComboBox classComboBox;
	private MyButton booksearchButton;
	private MyButton addbookButton;
	private MyButton deletebookButton;
	private MyButton modifybookButton;


	protected void initComponent() {
		try {
			Main.getLogic().searchBook();
		} catch (RemoteException e1) {
			e1.printStackTrace();
		}
		BookQueryPanelVO bqpvo=null;
		try {
			bqpvo = Main.getLogic().getBqpvo();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		
		logoutButton = new MyButton("注销");
		returnbackButton = new MyButton("返回");
		logoutButton.setBounds(MyFrame.WIDTH-80,0, 80, 20);
		returnbackButton.setBounds(MyFrame.WIDTH-160,0, 80, 20);
		this.add(logoutButton);
		this.add(returnbackButton);
		
		this.setBounds(300, 150, 700, 450);
		booklistLabel = new JLabel("图书列表：");
		booksearchLabel = new JLabel("精确查找：");
		bookdtm = new DefaultTableModel(bqpvo.getBookData(),new String[]{"书名","类别","作者","出版商","ISBN","价格","数量"});
		booklist = new JTable(bookdtm);
		DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();// 设置table内容居中
		tcr.setHorizontalAlignment(SwingConstants.CENTER);
		booklist.setDefaultRenderer(Object.class, tcr);
		bookjsp = new JScrollPane(booklist);
		bookjsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		booksearchField = new JTextField();
		classLabel = new JLabel("类别：");
		classComboBox = new JComboBox(new String[]{"书名","类别","作者","出版商","ISBN"});
		booksearchButton = new MyButton("精确查找");
		addbookButton = new MyButton("增添图书");
		deletebookButton = new MyButton("删除图书");
		modifybookButton = new MyButton("修改信息");
		booklistLabel.setBounds(10, 20, 100, 20);
		booksearchLabel.setBounds(400, 20, 100, 20);
		bookjsp.setBounds(10, 40, 350, 300);
		booksearchField.setBounds(390, 50, 200, 25);
		classLabel.setBounds(390, 100, 80, 25);
		classComboBox.setBounds(490, 100, 100, 25);
		booksearchButton.setBounds(480, 180, 100, 40);
		addbookButton.setBounds(385, 280, 90, 40);
		deletebookButton.setBounds(485, 280, 90, 40);
		modifybookButton.setBounds(585, 280,90, 40);
		this.add(booklistLabel);
		this.add(booksearchLabel);
		this.add(bookjsp);
		this.add(booksearchField);
		this.add(classLabel);
		this.add(classComboBox);
		this.add(booksearchButton);
		this.add(addbookButton);
		this.add(deletebookButton);
		this.add(modifybookButton);
		
		logoutButton.setButtonAction("logout");
		returnbackButton.setButtonAction("toMain");
		addbookButton.setButtonAction("toAddBook");
		deletebookButton.setButtonAction("deleteBook");
		modifybookButton.setButtonAction("toModifyBook");
		booksearchButton.setButtonAction("searchBook");
	}


	@Override
	protected void updateVO() {
		BookQueryPanelVO bqpvo=null;
		try {
			bqpvo = Main.getLogic().getBqpvo();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		bqpvo.setKeyword(booksearchField.getText());
		bqpvo.setKeywordType(classComboBox.getSelectedIndex());
		bqpvo.setSelectedRow(booklist.getSelectedRow());
		try {
			Main.getLogic().setBqpvo(bqpvo);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

}
